package com.example.webdemo.controller;

import com.example.webdemo.bean.base_dict;
import com.example.webdemo.dao.BaseDictDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dict")
public class BaseDictController {

    private final BaseDictDao baseDictDao;

    @Autowired
    public BaseDictController(BaseDictDao baseDictDao) {
        this.baseDictDao = baseDictDao;
    }

    // 添加字典项
    @PostMapping
    public ResponseEntity<?> addDict(@RequestBody base_dict dict) {
        try {
            int result = baseDictDao.addDict(dict);
            if (result > 0) {
                return ResponseEntity.ok().body("字典项添加成功");
            }
            return ResponseEntity.badRequest().body("字典项添加失败");
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("服务器错误: " + e.getMessage());
        }
    }

    // 删除字典项
    @DeleteMapping("/{dictId}")
    public ResponseEntity<?> deleteDict(@PathVariable Integer dictId) {
        try {
            int result = baseDictDao.deleteDict(dictId);
            if (result > 0) {
                return ResponseEntity.ok().body("字典项删除成功");
            }
            return ResponseEntity.notFound().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("服务器错误: " + e.getMessage());
        }
    }

    // 更新字典项
    @PutMapping
    public ResponseEntity<?> updateDict(@RequestBody base_dict dict) {
        try {
            int result = baseDictDao.updateDict(dict);
            if (result > 0) {
                return ResponseEntity.ok().body("字典项更新成功");
            }
            return ResponseEntity.notFound().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("服务器错误: " + e.getMessage());
        }
    }

    // 根据ID获取字典项
    @GetMapping("/{dictId}")
    public ResponseEntity<?> getDictById(@PathVariable Integer dictId) {
        try {
            base_dict dict = baseDictDao.getDictById(dictId);
            if (dict != null) {
                return ResponseEntity.ok(dict);
            }
            return ResponseEntity.notFound().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("服务器错误: " + e.getMessage());
        }
    }

    // 获取所有字典项
    @GetMapping("/all")
    public ResponseEntity<?> getAllDicts() {
        try {
            List<base_dict> dicts = baseDictDao.getAllDicts();
            return ResponseEntity.ok(dicts);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("服务器错误: " + e.getMessage());
        }
    }

    // 根据类型获取字典项
    @GetMapping("/type/{dictType}")
    public ResponseEntity<?> getDictsByType(@PathVariable String dictType) {
        try {
            List<base_dict> dicts = baseDictDao.getDictsByType(dictType);
            return ResponseEntity.ok(dicts);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("服务器错误: " + e.getMessage());
        }
    }

    // 根据类型和编码获取字典项
    @GetMapping("/type/{dictType}/code/{dictCode}")
    public ResponseEntity<?> getDictsByTypeAndCode(
            @PathVariable String dictType,
            @PathVariable String dictCode) {
        try {
            List<base_dict> dicts = baseDictDao.getDictsByTypeAndCode(dictType, dictCode);
            return ResponseEntity.ok(dicts);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("服务器错误: " + e.getMessage());
        }
    }

}
